package com.tencent.timsdk.emotion;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v4.view.PagerAdapter;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.jcp.baselibrary.utils.UIUtils;

import static com.tencent.timsdk.emotion.EmotionLayout.EMOJI_PER_PAGE;

/**
 * CSDN_LQR
 * 表情控件的ViewPager适配器(emoji + 贴图)
 */

public class  EmotionViewPagerAdapter extends PagerAdapter {

    int mPageCount = 0;
    int mTabPosi = 0;

    private IEmotionSelectedListener listener;
    EditText mMessageEditText;

    public void attachEditText(EditText messageEditText) {
        mMessageEditText = messageEditText;
    }

    public EmotionViewPagerAdapter(int tabPosi, IEmotionSelectedListener listener) {
        mTabPosi = tabPosi;

        if (mTabPosi == 0)
            mPageCount = (int) Math.ceil(EmotionManager.getDisplayCount() / (float) EMOJI_PER_PAGE);
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return mPageCount == 0 ? 1 : mPageCount;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        Context context = container.getContext();
        RelativeLayout rl = new RelativeLayout(context);
        rl.setGravity(Gravity.CENTER);

        GridView gridView = new GridView(context);
        gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        gridView.setLayoutParams(params);
        gridView.setGravity(Gravity.CENTER);

        gridView.setTag(position);//标记自己是第几页
        if (mTabPosi == 0) {
            gridView.setOnItemClickListener(emojiListener);
            gridView.setAdapter(new EmotionAdapter(context, position * EMOJI_PER_PAGE));
            gridView.setNumColumns(EmotionLayout.EMOJI_COLUMNS);
        }

        rl.addView(gridView);
        container.addView(rl);
        return rl;

    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public AdapterView.OnItemClickListener emojiListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            int index = position + (Integer) parent.getTag() * EMOJI_PER_PAGE;
            int count = EmotionManager.getDisplayCount();
            if (position == EMOJI_PER_PAGE || index >= count) {
                if (listener != null) {
                    listener.onEmotionSelected("/DEL");
                }
                onEmojiSelected(-1, "/DEL");
            } else {
                String text = EmotionManager.getDisplayText((int) id);
                if (!TextUtils.isEmpty(text)) {
                    if (listener != null) {
                        listener.onEmotionSelected(text);
                    }
                    onEmojiSelected((int)id, text);
                }
            }
        }
    };

    private void onEmojiSelected(int index, String key) {
        if (mMessageEditText == null)
            return;

        Editable editable = mMessageEditText.getText();
        if (key.equals("/DEL")) {
            mMessageEditText.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DEL));
        } else {
            try {
                Bitmap bitmap =  EmotionManager.getBitmap(UIUtils.getContext(), key);
                String content = String.valueOf(index);
                SpannableString str = new SpannableString(String.valueOf(index));
                ImageSpan span = new ImageSpan(UIUtils.getContext(), bitmap, ImageSpan.ALIGN_BOTTOM);
                str.setSpan(span, 0, content.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                mMessageEditText.append(str);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
